package com.das.mechanic_base.widget;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.res.ResourcesCompat;

import com.das.mechanic_base.R;
import com.das.mechanic_base.app.X3Constent;
import com.das.mechanic_base.base.X3BaseBottomSheetDialog;
import com.das.mechanic_base.utils.X3DateUtils;
import com.das.mechanic_base.utils.SpHelper;
import com.das.mechanic_base.utils.X3StringUtils;
import com.zyyoona7.wheel.WheelView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author chensi
 */
public class X3NumberAdminServiceDialog extends X3BaseBottomSheetDialog implements View.OnClickListener {

    private List<Integer> mList;

    private X3MarqueeText tv_title;
    private TextView tv_cancel;
    private TextView tv_affirm;
    private EditText et_input;
    private TextView tv_show;
    private TextView tv_miles;
    private WheelView<Integer> wv_num;

    private boolean isSave = false;
    private int currentIndex;
    private int currentMiles;
    private int recommendSafeMiles;
    private double perMiles;
    private String addDaysMonth;

    public X3NumberAdminServiceDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }

    @Override
    protected void initView() {
        tv_title = findViewById(R.id.tv_title);
        wv_num = findViewById(R.id.wv_num);
        tv_cancel = findViewById(R.id.tv_cancel);
        tv_affirm = findViewById(R.id.tv_affirm);
        et_input = findViewById(R.id.et_input);
        tv_show = findViewById(R.id.tv_show);
        tv_miles = findViewById(R.id.tv_miles);

        String km = (String) SpHelper.getData(X3Constent.MILEAGEUNIT, "km");
        tv_miles.setText(km);

        tv_cancel.setOnClickListener(this);
        tv_affirm.setOnClickListener(this);

        mList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            mList.add(i * 100);
        }

        for (int i = 1; i < 100; i++) {
            mList.add(i * 1000);
        }

        for (int i = 10; i <= 100; i++) {
            mList.add(i * 10000);
        }

        wv_num.setData(mList);
        wv_num.setTypeface(ResourcesCompat.getFont(mContext,R.font.din_text_type));
        wv_num.setOnItemSelectedListener((wheelView, data, position) -> {

            if (data <= currentMiles) {
                wv_num.setSelectedItemPosition(currentIndex);
                et_input.setText(currentMiles + "");
                et_input.setSelection(et_input.getText().toString().length());
                tv_show.setText(mContext.getString(R.string.x3_current_all_miles));
                return;
            }else {
                et_input.setText(data + "");
                et_input.setSelection(et_input.getText().toString().length());
            }

            /**
             * 需要计算时间
             */
            int afterDay = (int) ((data - currentMiles) / perMiles);

            addDaysMonth = X3DateUtils.getAddDaysMonth(afterDay);
            if (afterDay < 0) {
                int i = mList.indexOf(currentMiles);
                if (i > 0) {
                    wv_num.setSelectedItemPosition(i);
                    return;
                }
            }else if (afterDay == 0) {
                tv_show.setTextColor(Color.parseColor("#F64458"));
                tv_show.setText(mContext.getString(R.string.x3_now_to_work));
            }else {
                tv_show.setTextColor(Color.parseColor("#666666"));
                tv_show.setText(String.format(mContext.getString(R.string.x3_schedule_time), addDaysMonth));
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_number_picker_dialog;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_cancel) {
            dismiss();
        } else if (id == R.id.tv_affirm) {
            Integer selectedItemData = 0;
            String miles = et_input.getText().toString();
            if (X3StringUtils.isEmpty(miles)) {
                selectedItemData = wv_num.getSelectedItemData();
            } else {
                selectedItemData = Integer.parseInt(miles);
            }

            if (selectedItemData < currentMiles) {
                // TODO 不得小于当前行驶里程提醒
                return;
            }
            dismiss();

            if (iOnAffirmSelect != null) {
                iOnAffirmSelect.iOnAffirmMiles(selectedItemData);
            }
        }
    }

    IOnAffirmSelect iOnAffirmSelect;

    public void setiOnAffirmSelect(IOnAffirmSelect iOnAffirmSelect) {
        this.iOnAffirmSelect = iOnAffirmSelect;
    }

    public interface IOnAffirmSelect {

        void iOnAffirmMiles(int recommandMies);
    }

    public void changeData(String serviceName,String sn,int recommendMiles,int currentMiles,double perMile) {

        tv_title.setText(serviceName + "("+sn+")");
        tv_show.setText(mContext.getString(R.string.x3_to_tain_miles));

        this.recommendSafeMiles = recommendMiles;
        this.currentMiles = currentMiles;
        this.perMiles = perMile;

        int showMiles;

        currentIndex = mList.indexOf(currentMiles);
        if (currentIndex < 0) {
            mList.add(currentMiles);
            Collections.sort(mList);
            currentIndex = mList.indexOf(currentMiles);
        }

        if (recommendSafeMiles == currentMiles) {
            showMiles = currentMiles;
        }else {
            showMiles = recommendSafeMiles;
            /**
             * 需要计算时间
             */
            int afterDay = (int) ((recommendSafeMiles - currentMiles) / perMiles);

            if (afterDay < 0) {
                int i = mList.indexOf(currentMiles);
                if (i > 0) {
                    wv_num.setSelectedItemPosition(i);
                    return;
                }
            }else if (afterDay == 0) {
                tv_show.setTextColor(Color.parseColor("#F64458"));
                tv_show.setText(mContext.getString(R.string.x3_now_to_work));
            }else {
                String addDaysMonth = X3DateUtils.getAddDaysMonth(afterDay);
                tv_show.setTextColor(Color.parseColor("#666666"));
                tv_show.setText(String.format(mContext.getString(R.string.x3_schedule_time),addDaysMonth));
            }
        }
        setShowIndex(showMiles);
    }

    private void setShowIndex(int miles) {
        int i = mList.indexOf(miles);
        if (i > 0) {
            wv_num.setSelectedItemPosition(i);
        }else {
            mList.add(miles);
            Collections.sort(mList);
            i = mList.indexOf(miles);
            wv_num.setSelectedItemPosition(i);
        }
    }

}
